#!/usr/bin/env python
    ##########################################################################
    ##(c) 2008 Bastian Damman                                               ##
    ##                                                                      ##
    ##This file is part of Kaboom-O-rama.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is free software: you can redistribute it and/or modify ##
    ##it under the terms of the GNU General Public License as published by  ##
    ##the Free Software Foundation, either version 3 of the License, or     ##
    ##(at your option) any later version.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is distributed in the hope that it will be useful,      ##
    ##but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
    ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
    ##GNU General Public License for more details.                          ##
    ##                                                                      ##
    ##You should have received a copy of the GNU General Public License     ##
    ##along with Kaboom-O-rama.  If not, see <http://www.gnu.org/licenses/>.##
    ##########################################################################

from draw_func import *
from pygame import mouse, image, Surface, BLEND_RGBA_MULT
_SPEEDMIN = 2

class DropDown:
    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self._open = False
        self.items = []
        self.active_items = []
        self.selected = -1
        
        self.img = image.load("Form/Textures/DropDown/dropbox_top.png")
        draw_text(self.img, "DropDownBox", (6, 5), 20) 
        
    def add_item(self, text):
        self.items.append(DropDownItem(self, self.x, self.y, self.w, self.h, len(self.items) + 1, text))
        
    def open(self):
        self._open = True
        for i in self.items:
            if not i in self.active_items:
                self.active_items.append(i)
            i.open()
            
    def close(self):
        self._open = False
        for i in self.items:
            i.close()
            
    def remove_active(self, item):
        self.active_items.remove(item)
    
    def set_selected(self, index):
        index -= 1
        for i in range(len(self.active_items)):
            if i == index:
                self.active_items[i].selected = True
            else:
                self.active_items[i].selected = False
        self.selected = index
       
    def update(self, mouse_clicked):
        if mouse_clicked:
            pos = mouse.get_pos()
            if (pos[0] > self.x and pos[0] < self.x + self.w and
                pos[1] > self.y and pos[1] < self.y + self.h):
                    if not self._open:
                        self.open()
                    else:
                        self.close()
                    mouse_clicked = False #prevent bug in selection
            else:
                if self._open:
                    if not (pos[0] > self.x and pos[0] < self.x + self.w and
                        pos[1] > self.y + self.h and pos[1] < self.active_items[-1].y + self.h):
                        self.close()
            
        
        for i in self.active_items:
            i.update(mouse_clicked)
            
    def draw(self, screen):
        for i in self.active_items:
            i.draw(screen)
        screen.blit(self.img, (self.x, self.y))
    
class DropDownItem:
    def __init__(self, parent, x, y, w, h, index, text):
        self.x = x
        self.y = y
        self.ystart = y
        self.w = w
        self.h = h
        self.index = index
        self.text = text
        self._open = False
        self._close = False
        self.alpha = 1
        self.parent = parent
        self.selected = False
        self.hover = False
        
        self.img_normal = image.load("Form/Textures/DropDown/item_normal.png").convert_alpha()
        draw_text(self.img_normal, text, (4, 6), 18, (255, 255, 255))
        self.img_normal_hover = image.load("Form/Textures/DropDown/item_normal_hover.png").convert_alpha()
        draw_text(self.img_normal_hover, text, (4, 6), 18, (255, 255, 255))
        self.img_selected = image.load("Form/Textures/DropDown/item_selected.png").convert_alpha()
        draw_text(self.img_selected, text, (4, 6), 18, (255, 255, 255))
        self.img_selected_hover = image.load("Form/Textures/DropDown/item_selected_hover.png").convert_alpha()
        draw_text(self.img_selected_hover, text, (4, 6), 18, (255, 255, 255))
        
        
    def open(self):
        self.alpha = 1
        self._open = True
        self._close = False
        
    def close(self):
        self._close = True
        self._open = False
        
    def update(self, mouse_clicked):
        pos = mouse.get_pos()
        if (pos[0] > self.x and pos[0] < self.x + self.w and
            pos[1] > self.y and pos[1] < self.y + self.h):
            self.hover = True
            if mouse_clicked:
                self.parent.set_selected(self.index)
        else:
            self.hover = False
        
        if self._open:
            if self.y < self.ystart + self.index * self.h:
                self.y += _SPEEDMIN * self.index
            else:
                self.y == self.ystart + self.index * self.h
                self._open = False
        if self._close:
            if self.alpha > 0:
                self.alpha -= 0.1
                if self.alpha < 0:
                    self.alpha = 0
            else:
                self._close = False
                self.y = self.ystart
                self.parent.remove_active(self)
        
    def draw(self, screen):
        if self.selected:
            if self.hover:
                surf = self.img_selected_hover.copy()
            else:
                surf = self.img_selected.copy()
        else:
            if self.hover:
                surf = self.img_normal_hover.copy()
            else:
                surf = self.img_normal.copy()
        if self.alpha != 1:
            surf.fill((255, 255, 255, self.alpha * 255), None, BLEND_RGBA_MULT)
            
        screen.blit(surf, (self.x, self.y))